<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const arr = [1,2,3,4,5,6,3,2,1]
        const arr2 = [4,5,6,7,5,4]
        //1.数组去重
        console.log([...new Set(arr)])
        
        //2.数组求交集
        //第一步对arr数组去重
        // let result = [...new Set(arr)].filter(item=>{
        //     //对arr2进行去重 转换为对象
        //     let a = new Set(arr2) // 4 5 6 7
        //     if(a.has(item)){
        //         return true
        //     }else{
        //         return false
        //     }
        // })
        

        // 方法2的简写
        let result = [...new Set(arr)].filter( item =>  new Set(arr2).has(item))
        console.log('数组求交集：',result)

        //3.数组求并集
        let result2 = [...new Set([...arr,...arr2])]
        console.log('数组求并集：',result2)

        //4.数组求差集
        let result3 = [...new Set(arr)].filter( item =>  !(new Set(arr2).has(item)))
        console.log('数组求差集：',result3)
    </script>
    
</body>
</html>